<cfprocessingdirective suppresswhitespace="true">
<cfset gears_location = "#rereplace(rereplace(rereplace(cgi.SCRIPT_NAME,'/','.','ALL'),'.','','one'),'.ServiceInterface.cfm','', 'ALL')#">
<cfset migration_location = rereplace(gears_location,'(#application.env.context#[a-zA-Z\._-]+)$','', 'ALL') />

<!--- if application is under railo --->
<cfset gears_location = replacenocase(gears_location,"railo.","","ALL") />
<cfset migration_location = replacenocase(migration_location,"railo.","","ALL") />


<cfsetting showdebugoutput="false" enablecfoutputonly="true" />
<cfset variables.TurbineGears = createObject("component","#gears_location#.TurbineGears").init() />
<cfif (IsDefined("FORM.method") AND FORM.method EQ "getAvailableDSNs") OR (IsDefined("URL.method") AND URL.method EQ "getAvailableDSNs")>
	<cfoutput>#variables.TurbineGears.getAvailableDSNs()#</cfoutput>
<cfelseif (IsDefined("FORM.method") AND FORM.method EQ "getAvailableContexts") OR (IsDefined("URL.method") AND URL.method EQ "getAvailableContexts")>
	<cfoutput>#variables.TurbineGears.getAvailableContexts()#</cfoutput>
<cfelseif (IsDefined("FORM.method") AND FORM.method EQ "getAvailableModels") OR (IsDefined("URL.method") AND URL.method EQ "getAvailableModels")>
	<cfoutput>#variables.TurbineGears.getAvailableModels(form.ContextName)#</cfoutput>
<cfelseif (IsDefined("FORM.method") AND FORM.method EQ "getMigrations") OR (IsDefined("URL.method") AND URL.method EQ "getMigrations")>
	<cfoutput>#variables.TurbineGears.getAvailableMigrations(Form.ContextName)#</cfoutput>
<cfelseif (IsDefined("FORM.method") AND FORM.method EQ "ReverseEngineer") OR (IsDefined("URL.method") AND URL.method EQ "ReverseEngineer")>
	<cfset app_dot = rereplace(gears_location,'(#application.env.context#[a-zA-Z\._-]+)$','#form.context_name#', 'ALL') />
	<cfset object = createObject("component","#app_dot#.app.model.#form.model_name#").init() />
	<!--- WRITE LOG TO SEE IF APP_DOT is the CORRECT DOT PATH: cfset Application.Utils.Monitor.writeLogToFile("C:/tempLog.html", "#app_dot#") / --->
	<cfset objectPropertiesStruct = deserializeJSON(object.properties) />
	<cffunction name="recurseRead">
		<cfargument name="property">	
		<cfloop collection="#arguments.property#" item="i">
			<cfif arguments.property['#i#'] eq true>
				<cfset arguments.property['#i#'] = "true" />
			<cfelseif arguments.property['#i#'] eq false>
				<cfset arguments.property['#i#'] = "false" />
			</cfif>
			<cfswitch expression="#i#">
				<cfcase value="identity">
					<cfset tableStructure = tableStructure & "," & '"db_field_isIdentity":#arguments.property[i]#' />
				</cfcase>
				<cfcase value="mapping">
					<cfset tableStructure = tableStructure & "," & '"db_field_mapping":"#arguments.property[i]#"' />
				</cfcase>
				<cfcase value="default">
					<cfset tableStructure = tableStructure & "," & '"db_field_default":"#arguments.property[i]#"' />
				</cfcase>
				<cfcase value="key">
					<cfif arguments.property[i] eq 'primary'>
						<cfset tableStructure = tableStructure & "," & '"db_field_isPrimaryKey":true' />
					<cfelseif arguments.property[i] eq 'foreign'>
						<cfset tableStructure = tableStructure & "," & '"db_field_isForeignKey":true' />
					</cfif>
				</cfcase>
				<cfcase value="references">
					<cfset tableStructure = tableStructure & "," & '"db_field_refers_table":"#left(arguments.property[i],refind('\.',arguments.property[i])-1)#"' />
					<cfset tableStructure = tableStructure & "," & '"db_field_refers_column":"#mid(arguments.property[i],refind('\.',arguments.property[i])+1, len(arguments.property[i]))#"' />
				</cfcase>
				<cfcase value="type">
					<cfif arguments.property[i] eq "timestamp">
						<cfset tableStructure = tableStructure & "," & '"db_field_datatype":"datetime"' />
					<cfelseif arguments.property[i] eq "longvarchar">
						<cfset tableStructure = tableStructure & "," & '"db_field_datatype":"text"' />
					<cfelse>
					<cfset tableStructure = tableStructure & "," & '"db_field_datatype":"#arguments.property[i]#"' />
					</cfif>
				</cfcase>
				<cfcase value="maxlength">
					<cfset tableStructure = tableStructure & "," & '"db_field_length":"#arguments.property[i]#"' />
				</cfcase>
				<cfcase value="unique">
					<cfset tableStructure = tableStructure & "," & '"db_field_isUnique":#arguments.property[i]#' />
				</cfcase>
				<cfcase value="required">
					<cfif arguments.property['#i#'] eq true>
						<cfset arguments.property['#i#'] = "false" />
					<cfelseif arguments.property['#i#'] eq false>
						<cfset arguments.property['#i#'] = "true" />
					</cfif>
					<cfset tableStructure = tableStructure & "," & '"db_field_allowNull":#arguments.property[i]#' />
				</cfcase>	
			</cfswitch>
		</cfloop>
	</cffunction>
	<cfset json = "" />
	<cfloop collection="#objectPropertiesStruct[form.model_name]#" item="i">
		<cfset tableStructure = "{"/>
		<cfset tableStructure = tableStructure & '"db_field_name":"' & i & '"' />
		<cfset recurseRead(objectPropertiesStruct[form.model_name][i]) />
		<cfset tableStructure = tableStructure & "}"/>
		<cfset json = listAppend(json, tableStructure) />
	</cfloop>
	<cfoutput>[#json#]</cfoutput>
<cfelseif (IsDefined("FORM.method") AND FORM.method EQ "RunMigrations") OR (IsDefined("URL.method") AND URL.method EQ "RunMigrations")>
	<cftry>
	<cfset tmp = createObject('component','#migration_location##form.ContextName#.app.migrations.#form.MigrationName#').init() />
	<cfset tmp.setDSN(form.dsn_name) />
	
	<cfif Form.MigrationAction EQ 'up'>
		<cfset tmp.up() />
	<cfelseif Form.MigrationAction EQ 'down'>
		<cfset tmp.down() />
	</cfif>
		<cfoutput>[{"result":"success","detail":"#form.MigrationName#.#form.MigrationAction#()","message":"migration ran successfully","stacktrace":"#FORM.method#"}]</cfoutput>
	<cfcatch type="any">
		<cfoutput>[{"result":"failure","detail":"#cfcatch.detail#","message":"#cfcatch.Message#","stacktrace":"#cfcatch.detail#. #cfcatch.stacktrace#"}]</cfoutput>
	</cfcatch>
	</cftry>
</cfif>
</cfprocessingdirective>